package com.itheima.manager;

import cn.hutool.captcha.LineCaptcha;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import com.alibaba.fastjson.JSON;
import com.itheima.domain.db.Admin;
import com.itheima.exception.BusinessException;
import com.itheima.service.db.AdminService;
import com.itheima.util.ConstantUtil;
import com.itheima.util.JwtUtil;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import java.time.Duration;
import java.util.HashMap;
import java.util.Map;

@Component
public class AdminManager {

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @DubboReference
    AdminService adminService;

    public LineCaptcha getVerification(String uuid) {
        //1.使用hutool中api生成一张图片
        LineCaptcha lineCaptcha = new LineCaptcha(200,100);

        //2.将图片中验证码存入redis中,时效5’
        stringRedisTemplate.opsForValue().set(ConstantUtil.ADMIN_CODE+uuid, lineCaptcha.getCode(), Duration.ofMinutes(5));

        //3.返回图片
        return lineCaptcha;
    }

    public ResponseEntity login(String username, String password, String verificationCode, String uuid) {
        //1.获取redis中验证码,和用户输入的验证码进行比较,若不一致抛异常
        ValueOperations<String, String> operations = stringRedisTemplate.opsForValue();
        String redisCode = operations.get(ConstantUtil.ADMIN_CODE + uuid);
        if (!StrUtil.equalsAnyIgnoreCase(redisCode,verificationCode)) {
            throw new BusinessException("请输入正确的验证码");
        }

        //2.调用service通过用户名查找admin
        Admin admin = adminService.findByUsername(username);
        //3.若admin为空,抛异常
        if (admin == null) {
            throw new BusinessException("用户名输入错误");
        }

        //4.比较用户输入的密码和数据库中密码是否一致,若不一致抛异常
        if (!StrUtil.equals(admin.getPassword(), SecureUtil.md5(password))) {
            throw new BusinessException("密码输入错误");
        }

        //5.建议删除redis中验证码
        stringRedisTemplate.delete(ConstantUtil.ADMIN_CODE+uuid);

        //6.调用工具类生成token,使用hutool中工具类将admin转成map
        //把敏感数据置空
        admin.setPassword(null);
        String token = JwtUtil.createToken(BeanUtil.beanToMap(admin));

        //7.将token作为key,用户的json数组作为value写到redis中,时效1小时
        operations.set(ConstantUtil.ADMIN_TOKEN+token, JSON.toJSONString(admin),Duration.ofHours(1));

        //8.将token封装到map中返回
        Map<String,String> resultMap = new HashMap<>();
        resultMap.put("token",token);
        return ResponseEntity.ok(resultMap);
    }

    public void logout(String token) {
        token= token.replaceAll("Bearer ","");
        //删除redis中的管理员数据
        stringRedisTemplate.delete(ConstantUtil.ADMIN_TOKEN+token);
    }
}
